/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package ly.count.ohos.demo;

import ly.count.ohos.sdk.Countly;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ScrollView;
import ohos.global.configuration.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 自定义事件示例页面
 *
 * @since 2021-04-13
 */
public class AbilityExampleCustomEvents extends Ability {
    private static final String KEY_WALL = "wall";
    private static final String TAG = "AbilityExampleCustomEvents";
    private static final int MAX_INT = 2147483647;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_example_custom_events);
        ScrollView sv = (ScrollView) findComponentById(ResourceTable.Id_sv);
        sv.enableScrollBar(ScrollView.AXIS_Y, true);
        Countly.sharedInstance().onStart(this);
        findComponentById(ResourceTable.Id_button4).setClickedListener(this::onClickRecordEvent01);
        findComponentById(ResourceTable.Id_button10).setClickedListener(this::onClickRecordEvent02);
        findComponentById(ResourceTable.Id_button11).setClickedListener(this::onClickRecordEvent03);
        findComponentById(ResourceTable.Id_button12).setClickedListener(this::onClickRecordEvent04);
        findComponentById(ResourceTable.Id_button13).setClickedListener(this::onClickRecordEvent05);
        findComponentById(ResourceTable.Id_button14).setClickedListener(this::onClickRecordEvent06);
        findComponentById(ResourceTable.Id_button15).setClickedListener(this::onClickRecordEvent07);
        findComponentById(ResourceTable.Id_button16).setClickedListener(this::onClickRecordEvent08);
        findComponentById(ResourceTable.Id_button17).setClickedListener(this::onClickRecordEvent09);
        findComponentById(ResourceTable.Id_button18).setClickedListener(this::onClickRecordEvent10);
        findComponentById(ResourceTable.Id_button19).setClickedListener(this::onClickRecordEvent11);
        findComponentById(ResourceTable.Id_button67).setClickedListener(this::onClickRecordEvent12);
    }

    private void onClickRecordEvent01(Component v) {
        Countly.sharedInstance().events().recordEvent("Custom event 1");
    }

    private void onClickRecordEvent02(Component v) {
        final int count = 3;
        Countly.sharedInstance().events().recordEvent("Custom event 2", count);
    }

    private void onClickRecordEvent03(Component v) {
        final int count = 1;
        final int sum = 134;
        Countly.sharedInstance().events().recordEvent("Custom event 3", count, sum);
    }

    private void onClickRecordEvent04(Component v) {
        final int count = 1;
        final int sum = 0;
        final int dur = 55;
        Countly.sharedInstance().events().recordEvent("Custom event 4", null, count, sum, dur);
    }

    private void onClickRecordEvent05(Component v) {
        Map<String, Object> segmentation = new HashMap<>();
        segmentation.put(KEY_WALL, "green");
        final int count = 1;
        final int sum = 0;
        final int dur = 0;
        Countly.sharedInstance().events().recordEvent("Custom event 5", segmentation, count, sum, dur);
    }

    private void onClickRecordEvent06(Component v) {
        Map<String, String> segmentation = new HashMap<>();
        segmentation.put(KEY_WALL, "red");
        Map<String, Integer> segmentationInt = new HashMap<>();
        final int v1 = 3;
        segmentationInt.put("flowers", v1);
        Map<String, Double> segmentationDouble = new HashMap<>();
        final double v2 = 1.23;
        segmentationDouble.put("area", v2);
        final double v3 = 7.88;
        segmentationDouble.put("volume", v3);
        final int count = 15;
        final int sum = 0;
        final int dur = 0;
        Countly.sharedInstance().recordEvent("Custom event 6",
            segmentation, segmentationInt, segmentationDouble, count, sum, dur);
    }

    private void onClickRecordEvent07(Component v) {
        Map<String, String> segmentation = new HashMap<>();
        segmentation.put(KEY_WALL, "blue");
        Map<String, Integer> segmentationInt = new HashMap<>();
        segmentationInt.put("flowers", (int) (Math.random() * MAX_INT));
        Map<String, Double> segmentationDouble = new HashMap<>();
        segmentationDouble.put("area", Math.random());
        segmentationDouble.put("volume", Math.random());
        final int count = 25;
        final int sum = 10;
        final int dur = 0;
        Countly.sharedInstance().recordEvent("Custom event 7",
            segmentation, segmentationInt, segmentationDouble, count, sum, dur);
    }

    private void onClickRecordEvent08(Component v) {
        Map<String, Object> segmentation = new HashMap<>();
        segmentation.put(KEY_WALL, "yellow");
        final int count = 25;
        final int sum = 10;
        final int dur = 50;
        Countly.sharedInstance().events().recordEvent("Custom event 8", segmentation, count, sum, dur);
    }

    private void onClickRecordEvent09(Component v) {
        // start timed event
        Countly.sharedInstance().events().recordEvent("Custom event 9");
    }

    private void onClickRecordEvent10(Component v) {
        // stop timed event
        Countly.sharedInstance().events().recordEvent("Custom event 9");
    }

    private void onClickRecordEvent12(Component v) {
        // cancel timed event
        boolean isCancel = Countly.sharedInstance().events().cancelEvent("Custom event 9");
    }

    private void onClickRecordEvent11(Component v) {
        Map<String, Object> segmentation = new HashMap<>();
        segmentation.put(KEY_WALL, "orange");
        final int count = 4;
        final int sum = 34;
        Countly.sharedInstance().events().recordEvent("Custom event 9", segmentation, count, sum);
    }

    @Override
    public void onStop() {
        Countly.sharedInstance().onStop();
        super.onStop();
    }

    @Override
    public void onConfigurationUpdated(Configuration configuration) {
        super.onConfigurationUpdated(configuration);
        Countly.sharedInstance().onConfigurationChanged(configuration);
    }
}
